package com.security.demo.alipay.page.impl;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.*;
import com.security.core.properties.AliPayProperties;
import com.security.demo.alipay.page.AliPayPageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * AliPayServiceImpl
 *
 * @author: Dark_Blue
 * @date: 2020/3/13 18:18
 */
@Service
public class AliPayPageServiceImpl implements AliPayPageService {

    @Autowired
    private AliPayProperties aliPayProperties;

    @Override
    public String createOrder(String orderNo, String orderName, String amount, String description) {
        AlipayClient alipayClient = new DefaultAlipayClient(aliPayProperties.getGatewayUrl(), aliPayProperties.getAppid(),
                aliPayProperties.getAppPrivateKey(), aliPayProperties.getFormate(), aliPayProperties.getCharset(), aliPayProperties.getAlipayPublicKey(), aliPayProperties.getSignType());
        AlipayTradePagePayRequest aliPayRequest = new AlipayTradePagePayRequest();
        //获得初始化的AlipayClient
        //设置请求参数
        aliPayRequest.setReturnUrl(aliPayProperties.getReturnUrl());
        aliPayRequest.setNotifyUrl(aliPayProperties.getNotifyUrl());
        aliPayRequest.setBizContent("{\"out_trade_no\":\"" + orderNo + "\","
                + "\"total_amount\":\"" + amount + "\","
                + "\"subject\":\"" + orderName + "\","
                + "\"body\":\"" + description + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        //请求
        String result = null;
        try {
            result = alipayClient.pageExecute(aliPayRequest).getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public String getOrderInfo(String orderNo, String tradeNo) {
        AlipayClient alipayClient = new DefaultAlipayClient(aliPayProperties.getGatewayUrl(), aliPayProperties.getAppid(),
                aliPayProperties.getAppPrivateKey(), aliPayProperties.getFormate(), aliPayProperties.getCharset(), aliPayProperties.getAlipayPublicKey(), aliPayProperties.getSignType());
        AlipayTradeQueryRequest aliPayRequest = new AlipayTradeQueryRequest();
        aliPayRequest.setBizContent("{\"out_trade_no\":\"" + orderNo + "\"," + "\"trade_no\":\"" + tradeNo + "\"}");
        //请求
        String result = null;
        try {
            result = alipayClient.execute(aliPayRequest).getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public String refund(String orderNo, String tradeNo, String amount, String reason, String refundRequestNo) {
        AlipayClient alipayClient = new DefaultAlipayClient(aliPayProperties.getGatewayUrl(), aliPayProperties.getAppid(),
                aliPayProperties.getAppPrivateKey(), aliPayProperties.getFormate(), aliPayProperties.getCharset(), aliPayProperties.getAlipayPublicKey(), aliPayProperties.getSignType());
        //设置请求参数
        AlipayTradeRefundRequest aliPayRequest = new AlipayTradeRefundRequest();
        aliPayRequest.setBizContent("{\"out_trade_no\":\"" + orderNo + "\","
                + "\"trade_no\":\"" + tradeNo + "\","
                + "\"refund_amount\":\"" + amount + "\","
                + "\"refund_reason\":\"" + reason + "\","
                + "\"out_request_no\":\"" + refundRequestNo + "\"}");
        //请求
        String result = null;
        try {
            result = alipayClient.execute(aliPayRequest).getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public String refundQuery(String orderNo, String tradeNo, String refundRequestNo) {
        AlipayClient alipayClient = new DefaultAlipayClient(aliPayProperties.getGatewayUrl(), aliPayProperties.getAppid(),
                aliPayProperties.getAppPrivateKey(), aliPayProperties.getFormate(), aliPayProperties.getCharset(), aliPayProperties.getAlipayPublicKey(), aliPayProperties.getSignType());
        //设置请求参数
        AlipayTradeFastpayRefundQueryRequest aliPayRequest = new AlipayTradeFastpayRefundQueryRequest();
        aliPayRequest.setBizContent("{\"out_trade_no\":\""+ orderNo +"\","
                +"\"trade_no\":\""+ tradeNo +"\","
                +"\"out_request_no\":\""+ refundRequestNo +"\"}");
        //请求
        String result = null;
        try {
            result = alipayClient.execute(aliPayRequest).getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public String close(String orderNo, String tradeNo) {
        AlipayClient alipayClient = new DefaultAlipayClient(aliPayProperties.getGatewayUrl(), aliPayProperties.getAppid(),
                aliPayProperties.getAppPrivateKey(), aliPayProperties.getFormate(), aliPayProperties.getCharset(), aliPayProperties.getAlipayPublicKey(), aliPayProperties.getSignType());
        //设置请求参数
        AlipayTradeCloseRequest aliPayRequest = new AlipayTradeCloseRequest();
        aliPayRequest.setBizContent("{\"out_trade_no\":\""+ orderNo +"\"," +"\"trade_no\":\""+ tradeNo +"\"}");
        //请求
        String result = null;
        try {
            result = alipayClient.execute(aliPayRequest).getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return result;
    }

}
